import { createStore } from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import user from './modules/user';

export default createStore({
  state: {
    token: '',
    isLoggedIn: false
  },
  getters: {
    getToken: (state) => {
      return state.token;
    },
    getIsLoggedIn: (state) => {
      return state.isLoggedIn;
    }
  },
  mutations: {
    setToken: (state, token) => {
      state.token = token;
    },
    setIsLoggedIn: (state, isLoggedIn) => {
      state.isLoggedIn = isLoggedIn;
    }
  },
  actions: {
    updateToken: ({ commit }, token) => {
      commit('setToken', token);
    },
    updateIsLoggedIn: ({ commit }, isLoggedIn) => {
      commit('setIsLoggedIn', isLoggedIn);
    }
  },
  modules: {
    user
  },
  plugins: [createPersistedState()] // 应用 vuex-persistedstate 插件
})
